<template>
  <div>
    <h1>商品信息列表</h1>
    <div>
      <el-button type="primary" @click="onAddProdInfo">商品信息添加</el-button>
    </div>
    <div style="margin-top: 10px">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="infoId" label="id" width="60" />
        <el-table-column prop="prodSku" label="商品Sku" width="100" />
        <el-table-column prop="prodName" label="商品名称" width="100" />
        <el-table-column prop="prodPrice" label="商品价格" width="100" />
        <el-table-column prop="costPrice" label="价格" width="100" />
        <el-table-column prop="prodStock" label="库存" width="100" />
        <el-table-column label="操作" />
      </el-table>
    </div>
    <div style="margin-top: 10px">
      <div class="demo-pagination-block">
        <el-pagination
          v-model:current-page="searchForm.pageNum"
          v-model:page-size="searchForm.pageSize"
          :page-sizes="[2, 4, 6, 8]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <div>
      <el-dialog v-model="dialogFormVisible" title="添加" width="500">
        <el-form :model="form">
          <el-form-item label="商品" :label-width="formLabelWidth">
            <el-select v-model="form.prodId" placeholder="请选择商品">
              <el-option
                v-for="item in prodList"
                :label="item.prodName"
                :value="item.prodId"
                :key="item.prodId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="商品信息" :label-width="formLabelWidth">
            <el-input v-model="form.prodName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="商品SKU" :label-width="formLabelWidth">
            <el-input v-model="form.prodSku" autocomplete="off" />
          </el-form-item>
          <el-form-item label="商品成本价" :label-width="formLabelWidth">
            <el-input v-model="form.costPrice" autocomplete="off" />
          </el-form-item>
          <el-form-item label="商品售价" :label-width="formLabelWidth">
            <el-input v-model="form.prodPrice" autocomplete="off" />
          </el-form-item>
          <el-form-item label="商品库存" :label-width="formLabelWidth">
            <el-input v-model="form.prodStock" autocomplete="off" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="addInfo"> 提交 </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import { getProdInfoPage, addProdInfo, getProdList } from "../api/api";
import { ElMessage } from "element-plus";

const tableData = ref([]);
const total = ref(0);
const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
});

const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const form = ref({
  buyNum: 0,
  costPrice: 0,
  prodId: null,
  prodName: "",
  prodPrice: 0,
  prodSku: "",
  prodStock: 0,
});
const prodList = ref([]);

//确认添加
const addInfo = () => {
  addProdInfo(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      loadProdInfoList();
      dialogFormVisible.value = false;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

//显示商品信息添加对话框
const onAddProdInfo = () => {
  console.log("onAddProdInfo");
  dialogFormVisible.value = true;

  form.value = {
    buyNum: 0,
    costPrice: 0,
    prodId: null,
    prodName: "",
    prodPrice: 0,
    prodSku: "",
    prodStock: 0,
  };
};

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadProdInfoList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadProdInfoList();
};

const loadProdInfoList = () => {
  getProdInfoPage(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

onMounted(() => {
  loadProdInfoList();
  getProdList({ pageNum: 1, pageSize: 9999 }).then((res) => {
    prodList.value = res.data.data.records;
  });
});
</script>

<!-- 分页 -->
<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>
